package com.example.shoppingcenter.Controller;

import com.example.shoppingcenter.Ai.ClipDrop.ImageVO;
import com.example.shoppingcenter.POJO.Chat;
import com.example.shoppingcenter.POJO.Image;
import com.example.shoppingcenter.POJO.User;
import com.example.shoppingcenter.Service.userService;
import com.example.shoppingcenter.Service.xfService;
import com.example.shoppingcenter.Service.cdService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class htmlController {

    @Resource
    private userService userService;

    @Resource
    private xfService xfService;

    @Resource
    private cdService cdService;

    @RequestMapping("/")
    public String redirectToHome() {
        return "redirect:/home";
    }

    @RequestMapping("/home")
    public String showHome() {
        return "home";
    }

    @RequestMapping("/login")
    public String showLoginPage() {
        return "user/login"; // 这里返回的字符串是模板文件的名称（不包括文件扩展名）
    }

    @RequestMapping("/chat")
    public String showChatPage(HttpServletRequest request, Model model) {
        User user = userService.getUserByAccount((String) request.getSession().getAttribute("user"));
        List<Chat> chatList = xfService.getChatList(user.getId());
        model.addAttribute("chatList",chatList);
        model.addAttribute("user", user);
        return "chat";
    }

    @RequestMapping("/image")
    public String showImagePage(HttpServletRequest request,Model model) {
        User user = userService.getUserByAccount((String) request.getSession().getAttribute("user"));
        model.addAttribute("user",user);
        model.addAttribute("apiKey",cdService.getKey());
        return "image";
    }

    @RequestMapping("/register")
    public String showRegister() {
        return "user/register";
    }

    @RequestMapping("/detail")
    public String showUser(HttpServletRequest httpServletRequest, Model model) {
        HttpSession session = httpServletRequest.getSession();
        String account = (String) session.getAttribute("user");
        User user = userService.getUserByAccount(account);
        model.addAttribute("user", user);
        return "user/detail";
    }

    @RequestMapping("/changepw")
    public String showpwd(){
        return "user/changepw";
    }

    @RequestMapping("/collects")
    public String showCollects(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        String account = (String) session.getAttribute("user");
        User user = userService.getUserByAccount(account);
        List<ImageVO> images = cdService.getImagesByUserId(user.getId());
        model.addAttribute("imageList",images);
        return "/collects";
    }
}
